package com.wman.sheeplib;

import android.content.Context;
import android.os.Environment;

import com.wman.sheep.common.application.AbsAppConfig;
import com.wman.sheep.common.exception.BaseException;
import com.wman.sheep.common.utils.CacheDirectoryUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * 应用相关配置项
 */

public class AppConfig extends AbsAppConfig{
    private final static String APP_CONFIG = "config";
    //服务器地址
    public static final String SERVER_HEADER = "http://apiv2.xiaorizi.me";
    public static final String BETA_SERVER_HEADER = SERVER_HEADER;

    // 存放详情图片的路径
    public final static String SAVE_IMAGE_PATH = Environment
            .getExternalStorageDirectory()
            + File.separator
            +"xiaorizi"
            + File.separator + "img" + File.separator;
    /**
     * 分页 每页的数量
     */
    public static final int LIMIT = 12;

    // 默认存放图片的路径
    public final static String DEFAULT_SAVE_IMAGE_PATH =
            CacheDirectoryUtil.getCacheDirectory(AppContext.context(), null)
                    + File.separator + "img" + File.separator;

    public final static String DEFAULT_SLAPE_IMG_NAME = "slape.jpg";
    public final static String DEFAULT_SLAPE_FILE_PATH = DEFAULT_SAVE_IMAGE_PATH + DEFAULT_SLAPE_IMG_NAME;

    private Context mContext;
    private static AppConfig appConfig;

    public static AppConfig getAppConfig() {
        if (appConfig == null) {
            appConfig = new AppConfig();
            appConfig.mContext = AppContext.context();
        }
        return appConfig;
    }

    public String get(String key) throws BaseException {
        Properties props = get();
        if (props != null) {
            String value = props.getProperty(key);
            if (value == null) {
                throw new BaseException("没有登录或注册过");
            } else {
                return value;
            }
        } else {
            throw new BaseException("App缓存异常");
        }
    }

    public Properties get() {
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            // 读取files目录下的config
            // fis = activity.openFileInput(APP_CONFIG);

            // 读取app_config目录下的config
            File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
            fis = new FileInputStream(dirConf.getPath() + File.separator
                    + APP_CONFIG);

            props.load(fis);
        } catch (Exception e) {
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return props;
    }

    private void setProps(Properties p) {
        FileOutputStream fos = null;
        try {
            // 把config建在files目录下
            // fos = activity.openFileOutput(APP_CONFIG, Context.MODE_PRIVATE);

            // 把config建在(自定义)app_config的目录下
            File dirConf = mContext.getDir(APP_CONFIG, Context.MODE_PRIVATE);
            File conf = new File(dirConf, APP_CONFIG);
            fos = new FileOutputStream(conf);

            p.store(fos, null);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

    public void set(Properties ps) {
        Properties props = get();
        props.putAll(ps);
        setProps(props);
    }

    public void set(String key, String value) {
        Properties props = get();
        props.setProperty(key, value);
        setProps(props);
    }

    public void remove(String... key) {
        Properties props = get();
        for (String k : key)
            props.remove(k);
        setProps(props);
    }
}